package com.aptech.campaign.component;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.aptech.campaign.entities.Report;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JPanel;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.view.JRViewer;

public class ReportManager {

    @SuppressWarnings(value = "unchecked")
    public JPanel getEnumerationViewer(ArrayList<Report> arr, boolean isEnumeration) {
        JPanel viewer = null;

        try {

            HashMap parameterMap = new HashMap();

            parameterMap.put("CAMPAIGN", "CAMPAIGN NAME");
            parameterMap.put("PRODUCT", "PRODUCT NAME");
            parameterMap.put("SALETARGET", "SALE TARGET ($)");
            parameterMap.put("ACTUALSALE", "ACTUAL SALE ($)");
            parameterMap.put("ESTIMATEVARIANCE", "ESTIMATE VARIANCE (%)");
            parameterMap.put("ACTUALVARIANCE", "ACTUAL VARIANCE (%)");
//             parameterMap.put("HOTEN", "HO VA TEN");
//            parameterMap.put("TUOI", "TUOI");
//            parameterMap.put("KHOAHOC", "KHOA HOC");
//            parameterMap.put("HOCPHI", "HOC PHI");

            ArrayList reportRows = new ArrayList();

            HashMap rowMap = null;
            for (Report re : arr) {
                rowMap = new HashMap();
                rowMap.put("CAMPAIGN", re.getCampaign());
                rowMap.put("PRODUCT", re.getProduct());
                rowMap.put("SALETARGET", new Float(re.getSaleTarget()));
                rowMap.put("ACTUALSALE", new Float(re.getActualSale()));
                rowMap.put("ESTIMATEVARIANCE", new Float(re.getEstimateVariance()));
                rowMap.put("ACTUALVARIANCE", new Float(re.getActualVariance()));
//
//                rowMap.put("HOTEN", "adad");
//                rowMap.put("TUOI", 23);
//                rowMap.put("KHOAHOC", "fafa");
//                rowMap.put("HOCPHI", 21F);

                reportRows.add(rowMap);
            }
            //rowMap = new HashMap();


            JasperPrint jasperPrint = JasperFillManager.fillReport(getInputStream("Report.jasper"),
                    parameterMap, new JRMapCollectionDataSource(reportRows));
            viewer = new JRViewer(jasperPrint);
        } catch (Exception ex) {
            System.out.println("Error!" + ex.getMessage());
        }

        return viewer;
    }

    private InputStream getInputStream(String reportName) {
        URL url = getClass().getResource("../report/" + reportName);
        InputStream input = null;

        try {
            input = url.openStream();
            return input;
        } catch (IOException ex) {
            return null;
        }
    }
}
